package com.yuuzy.view;

import com.yuuzy.dao.Buy_WipDAO;
import com.yuuzy.model.Buy_Wip;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

public class VieworderWip extends JInternalFrame {
    private JTable wip_table;

    private Buy_WipDAO buy_wipDAO = new Buy_WipDAO();
    private JTextField wip_idText;
    private JTextField client_idText;

    /**
     * Create the frame.
     */
    public VieworderWip() {
        setTitle("查看理财产品订单");
        setClosable(true);
        setIconifiable(true);
        setBounds(100, 100, 793, 741);

        JScrollPane scrollPane = new JScrollPane();

        JLabel lblNewLabel = new JLabel("理财产品编号：");

        JLabel lblNewLabel_1 = new JLabel("客户编号：");

        wip_idText = new JTextField();
        wip_idText.setColumns(10);

        client_idText = new JTextField();
        client_idText.setColumns(10);

        JButton btnNewButton = new JButton("查询 ");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                queryActionPerformed(e);
            }
        });
        btnNewButton.setIcon(new ImageIcon(VieworderWip.class.getResource("/image/pm-query-16.png")));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.TRAILING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(106)
                                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 553, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(187)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(lblNewLabel)
                                                        .addComponent(lblNewLabel_1))
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addGap(10)
                                                                .addComponent(wip_idText, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addGap(9)
                                                                .addComponent(client_idText, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)))))
                                .addContainerGap(118, Short.MAX_VALUE))
                        .addGroup(groupLayout.createSequentialGroup()
                                .addContainerGap(500, Short.MAX_VALUE)
                                .addComponent(btnNewButton)
                                .addGap(188))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.TRAILING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(59)
                                                .addComponent(lblNewLabel)
                                                .addGap(6)
                                                .addComponent(btnNewButton))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(53)
                                                .addComponent(wip_idText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                                .addGap(8)
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addComponent(lblNewLabel_1)
                                        .addComponent(client_idText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 439, GroupLayout.PREFERRED_SIZE)
                                .addGap(70))
        );

        wip_table = new JTable();
        wip_table.setFillsViewportHeight(true);
        wip_table.setModel(new DefaultTableModel(
                new Object[][]{
                },
                new String[]{
                        "理财产品编号", "客户id", "购买时间", "购买数量", "资产状态", "收益"
                }
        ));
        wip_table.getColumnModel().getColumn(1).setPreferredWidth(93);
        wip_table.getColumnModel().getColumn(2).setPreferredWidth(127);
        wip_table.getColumnModel().getColumn(3).setPreferredWidth(158);
        wip_table.getColumnModel().getColumn(4).setPreferredWidth(189);
        wip_table.getColumnModel().getColumn(5).setPreferredWidth(161);
        scrollPane.setViewportView(wip_table);
        getContentPane().setLayout(groupLayout);

        this.fillTable(new Buy_Wip());
    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    VieworderWip frame = new VieworderWip();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 查询事件处理
     *
     * @param evt
     */
    private void queryActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String wip_id = this.wip_idText.getText();
        String client_id = this.client_idText.getText();
        Buy_Wip buy_wip = new Buy_Wip(Integer.parseInt(wip_id), Integer.parseInt(client_id));
        this.fillTable(buy_wip);
    }

    private void fillTable(Buy_Wip buy_wip) {
        DefaultTableModel dtm = (DefaultTableModel) wip_table.getModel();
        dtm.setRowCount(0);//设置为0行，清空表格
        List<Buy_Wip> orderList = buy_wipDAO.queryBuy_Wip(buy_wip);
        for (Buy_Wip rs : orderList) {
            Vector v = new Vector();
            v.add(rs.getWip_id());
            v.add(rs.getClient_id());
            v.add(rs.getBuy_wip_time());
            v.add(rs.getBuy_wip_quantity());
            v.add(rs.getBuy_wip_assetstatus());
            v.add(rs.getBuy_wip_earnings());
            dtm.addRow(v);
        }
    }

}
